$(document).ready(doinit);

function doinit() {
	$('#search').click(function() {
		$('#toshowContent').css('display', '');
		$('#toshowContentPic').css('display', 'none');
		
		doSearch();
		
		showHistoryPanel();
	});
	
	$('#searchPic').click(function () {
		$('#toshowContent').css('display', 'none');
		$('#toshowContentPic').css('display', '');
		showPicContent();
		
		showHistoryPanel();
	});
	
	showPicContent();
	$('#toshowContentPic').css('display', '');
}

function showHistoryPanel() {
	$('#historyWin').window({
		collapsible:false,
	    minimizable:false,
	    maximizable:false,
	    closable:false,
		closed:false,
		tools:[{
	        iconCls:'icon-cancel',
	        handler:function(){
	        	$('#historyWin').window("close");
	        }
   		 }]
	});
}

function showPicContent() {
		var options = {
			appCode : $('#apparatusInfo').attr('appCode'),
			startTime : $.trim($('#startTimeHis').val()),
			endTime : $.trim($('#endTimeHis').val())
		};
		
		$.getJSON("appointment!showAppointmentInPic.action", options, function(data){
			  $('#toshowContentPic').empty();
			  
	          var tb = $('#picTable').clone().attr("id", "");
			  $.each(data,function(key,values){
			  	 var tr = $('<tr></tr>');
			  	 var td = $('<td align="center">' + key + '</td>');
			  	 tr.append(td);
			  	 
			  	 $.each(values,function(hour, color) {
				  	 	//var tdhour = $('<td style="background: ' + color + '">' + hour + '</td>');
				  	 	var tdhour = $('<td style="background: ' + color + '"> &nbsp;</td>');	
				  	 	tr.append(tdhour);
			  	 });
			  	 
			  	 tb.append(tr);
			  });
			  
			  var head = $('<center><span><b>绿色表示审批通过，橙色表示待审核，红色表示仪器不可用</b></span></center>');
			  $('#toshowContentPic').append(head);
			  $('#toshowContentPic').append(tb);
     		});
}

function doSearch() {

	showContent({
		startTime : $.trim($('#startTimeHis').val()),
		endTime : $.trim($('#endTimeHis').val()),
		appCode : $('#apparatusInfo').attr('appCode'),
		historys : 'yes'
	}, '#appointments', true);
}

/**
 * 显示信息历史信息
 */
function showContent(queryParams, table, flag) {
	$(table).datagrid({
	    title:'',
	    iconCls:'icon-save',
	    singleSelect:true,
	    url:'appointment!queryDetailHistory.action',
	    queryParams :queryParams,
	    columns:[[
	        {field:'name',title:'仪器名称',width:fixWidth(0.08)},
	        {field:'operatNo',title:'订单号' ,width:fixWidth(0.08) , hidden : flag},
	        {field:'loginAccount',title:'预约者登录名',width:fixWidth(0.1)},
	        {field:'phone',title:'联系方式', hidden : flag,width:fixWidth(0.1)},
	        {field:'startTime',title:'预约开始时间',width:fixWidth(0.1)},
	        {field:'endTime', title:'预约结束时间',width:fixWidth(0.1)},
	        {field:'useType',title:'使用情况',hidden : 'true'},
	        {field:'checkStatus',title:'状态',align:'center' ,width:fixWidth(0.1),
	        	    formatter : function(value, rowData, rowIndex) {
	        	    	if (rowData.appType == 0) {
							if (value == 1) {
								return '待审核';
					 		} 
					 		else if (value == 2) {
								return '课题组长审核通过';
					 		} 
					 		else if (value == 3) {
								return '课题组长审核拒绝';
					 		} 
					 		else if (value == 4) {
								return '管理员审核通过';
					 		} 
					 		else if (value == 5) {
								return '管理员审核拒绝';
					 		} else {
					 			return '未知'
					 		}
					 	} else if(rowData.appType == 1)  {
	        	    		return '已取消';
	        	    	} 
					}
			},
			{field:'useType',title:'使用情况',align:'center',width:fixWidth(0.05),hidden : flag,
	        	    formatter : function(value, rowData, rowIndex) {
						if (value == 1) {
							return '预约';
				 		} 
				 		else if (value == 2) {
							return '超时使用';
				 		} 
				 		else if (value == 3) {
							return '超时未用';
				 		} 
				 		else if (value == 4) {
							return '已经使用';
				 		} else {
				 			return '未知'
				 		}
					}
			},
			{field:'remark', title:'备注', width:fixWidth(0.2), hidden : flag}
	    ]],
	    pageList:[5, 10],
	    pagination:true,
	    rownumbers:true,
	    fitColumns : true
	});
}